<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">
          程亿保管理平台
        </h3>
        <lang-select class="set-language" />
      </div>

      <el-form-item prop="agentId">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="loginForm.agentId"
          :placeholder="$t('login.username')"
          name="agentId"
          type="text"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item v-if="loginForm.loginType===0" prop="agentAuth" :rules="[{ required: true, message: '密码不能为空', trigger: 'blur' }]">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.agentAuth"
          :type="passwordType"
          placeholder="请输入密码"
          name="agentAuth"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item v-if="loginForm.loginType===1" prop="agentAuth" :rules="[{ required: true, message: '验证码不能为空', trigger: 'blur' }]">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.agentAuth"
          type="text"
          placeholder="请输入验证码"
          name="agentAuth"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd">
          <count-down :phone="loginForm.agentId" />
        </span>
      </el-form-item>
      <el-form-item v-if="captcha.required" prop="imgValidCodeText" :rules="[{ required: true, message: '验证码必填', trigger: 'blur' }]">
        <div class="el-input">
          <span>
            <img :src="captcha.imgText" style="position: relative; top: 6px; left: 2px;" @click="initCaptcha">
            <input v-model="loginForm.imgValidCodeText" style="position: relative; top: -8px;">
          </span>
        </div>
      </el-form-item>
      <div class="login-type">
        <el-button type="text" size="mini" @click="changeLoginType">{{ loginForm.loginType===0?'切换手机号登陆':'切换密码登陆' }}</el-button>
      </div>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;margin-left:0px;" @click.native.prevent="handleLogin">
        {{ $t('login.logIn') }}
      </el-button>
    </el-form>

    <el-dialog :title="$t('login.thirdparty')" :visible.sync="showDialog">
      {{ $t('login.thirdpartyTips') }}
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog>
    <div class="dec" />
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect';
import CountDown from '@/components/countDown';
import { create } from '@/api/admin/captcha';

export default {
  name: 'Login',
  components: { LangSelect, CountDown },
  data() {
    return {
      loginForm: {
        loginType: 0,
        agentId: null,
        agentAuth: null,
        imgValidCodeText: ''
      },
      loginRules: {
        agentId: [{ required: true, message: '账号不能为空', trigger: 'blur' }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      captcha: {
        required: false,
        id: null,
        imgText: null
      }
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
    this.initCaptcha();
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {

    changeLoginType() {
      this.loginForm.agentAuth = '';
      this.loginForm.loginType = this.loginForm.loginType === 0 ? 1 : 0;
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const params = {
            loginType: this.loginForm.loginType,
            agentId: this.loginForm.agentId,
            agentAuth: this.loginForm.agentAuth,
            imgValidCodeId: this.captcha.id,
            imgValidCodeText: this.loginForm.imgValidCodeText
          };
          this.$store.dispatch('LoginByUsername', params).then(() => {
            this.loading = false;
            this.$router.push({ path: this.redirect || '/' });
          }).catch((error) => {
            this.loading = false;
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    initCaptcha() {
      const size = {
        width: 120,
        height: 40
      };
      create(size).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.captcha = result.data;
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">

  $bg:#283443;
  $light_gray:#eee;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input{
      color: $cursor;
      &::first-line {
        color: $light_gray;
      }
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-type{
    margin-bottom: 22px;
    display: flex;
    justify-content: flex-end;
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size:18px;
      right: 0px;
      cursor: pointer;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
    .dec{
    width: 100%;
    font-size: 12px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    bottom: 0;
    text-align: center;
    .info{
      margin-right: 10px;
    }
  }
}
</style>
